package com.cike.productmanager;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.cike.productmanager.DBHelper.DBManager;
import com.cike.productmanager.adt.Store;
import com.cike.productmanager.util.DataApplication;

public class MainPad extends Activity {
	public static MainPad instance = null;
	 
	private ViewPager mTabPager;	
	private ImageView mTabImg;// 动画图片
	private ImageView mTab1,mTab2,mTab3,mTab4,mTab5;
	private int zero = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int one;//单个水平动画位移
	private int two;
	private int three;
	private int four;
	
	
	private Button searchbutton;
	private Spinner districtSp;
	private Spinner streetSp;
	private Spinner markettypeSp;
	
	
	//记录选择了哪一个选项卡
	private String districtselection;
	private String streetselection;
	private String markettypeselection;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepad);
         //启动activity时不自动弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
        instance = this;
        
        mTabPager = (ViewPager)findViewById(R.id.tabpager);
        mTabPager.setOnPageChangeListener(new MyOnPageChangeListener());
        
        mTab1 = (ImageView) findViewById(R.id.img_home);
        mTab2 = (ImageView) findViewById(R.id.img_visit);
        mTab3 = (ImageView) findViewById(R.id.img_store);
        mTab4 = (ImageView) findViewById(R.id.img_custome);
        mTab5 = (ImageView) findViewById(R.id.img_settings);
        
        mTabImg = (ImageView) findViewById(R.id.img_tab_now);
        mTab1.setOnClickListener(new MyOnClickListener(0));
        mTab2.setOnClickListener(new MyOnClickListener(1));
        mTab3.setOnClickListener(new MyOnClickListener(2));
        mTab4.setOnClickListener(new MyOnClickListener(3));
        mTab5.setOnClickListener(new MyOnClickListener(4));
        
        Display currDisplay = getWindowManager().getDefaultDisplay();//获取屏幕当前分辨率
        int displayWidth = currDisplay.getWidth();
        one = displayWidth/5; //设置水平动画平移大小
        two = one*2;
        three = one*3;
        four = one*4;
        //Log.i("info", "获取的屏幕分辨率为" + one + two + three + "X" + displayHeight);
        
        //InitImageView();//使用动画
      //将要分页显示的View装入数组中
        LayoutInflater mLi = LayoutInflater.from(this);
        View view1 = mLi.inflate(R.layout.main_tab_home, null);
        View view2 = mLi.inflate(R.layout.main_tab_visit, null);
        View view3 = mLi.inflate(R.layout.main_tab_store, null);
        View view4 = mLi.inflate(R.layout.main_tab_customer, null);
        View view5 = mLi.inflate(R.layout.main_tab_setting, null);
        
      //每个页面的view数据
        final ArrayList<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);
        views.add(view5);
        
        
        
      //填充ViewPager的数据适配器
        PagerAdapter mPagerAdapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}
			
			@Override
			public int getCount() {
				return views.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager)container).removeView(views.get(position));
			}
			

			
			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager)container).addView(views.get(position));
				return views.get(position);
			}
		};
		
		mTabPager.setAdapter(mPagerAdapter);
		
		
		districtSp = (Spinner)view2.findViewById(R.id.districtSpinner);
		streetSp = (Spinner)view2.findViewById(R.id.streetSpinner);
		markettypeSp = (Spinner)view2.findViewById(R.id.marketSpinner);
        searchbutton = (Button)view2.findViewById(R.id.searchButton);
        
        DBManager.openDB(getBaseContext());
        
        ArrayList<String> districtname = new ArrayList<String>();
        ArrayList<String> streetname = new ArrayList<String>();
        ArrayList<String> typename = new ArrayList<String>();
        
        districtname.add("任意");
        streetname.add("任意");
        typename.add("任意");
        
        for(Store storeitem: DataApplication.storeList){
        	if(!districtname.contains(storeitem.getDistrict()))districtname.add(storeitem.getDistrict());
        	if(!streetname.contains(storeitem.getStreet()))streetname.add(storeitem.getStreet());
        	if(!typename.contains(storeitem.getStoreType()))typename.add(storeitem.getStoreType());
        }
        
        ArrayAdapter< String> districtAdapter =new ArrayAdapter< String>( this,android.R.layout.select_dialog_item);
        for(String item:districtname){
        	districtAdapter.add(item);
        }
        districtSp.setAdapter(districtAdapter);
        
        ArrayAdapter< String> streetAdapter =new ArrayAdapter< String>( this,android.R.layout.select_dialog_item);
        for(String item:streetname){
        	streetAdapter.add(item);
        }
        streetSp.setAdapter(streetAdapter);
        
        ArrayAdapter< String> markettypeAdapter =new ArrayAdapter< String>( this,android.R.layout.select_dialog_item);
        for(String item:typename){
        	markettypeAdapter.add(item);
        }
        markettypeSp.setAdapter(markettypeAdapter);
        
        
        
        
        
        searchbutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				//获取当前选项
				districtselection = districtSp.getSelectedItem().toString();
				streetselection = streetSp.getSelectedItem().toString();
				markettypeselection = markettypeSp.getSelectedItem().toString();
				
				
				
				//用数组存储搜索出来的门店在StoreList里面的index和店名
				ArrayList<Integer> index = new ArrayList<Integer>();
				ArrayList<String> name = new ArrayList<String>();
				for(int i=0;i<DataApplication.storeList.size();i++){
					if(
							(DataApplication.storeList.get(i).getDistrict().equals(districtselection)||districtselection.equals("任意"))
							&&(DataApplication.storeList.get(i).getStreet().equals(streetselection)||streetselection.equals("任意"))
							&&(DataApplication.storeList.get(i).getStoreType().equals(markettypeselection)||markettypeselection.equals("任意"))
							){
						index.add(i);
						name.add(DataApplication.storeList.get(i).getStorename());
					}
				}
				if (index.size() > 0) {
					final int size = name.size();
					final String[] areas = (String[]) name.toArray(new String[size]);
					final Integer[] indexs = (Integer[]) index.toArray(new Integer[size]);
				
				
					new AlertDialog.Builder(MainPad.this).setTitle("选择门店").setItems(areas,new DialogInterface.OnClickListener(){
					      public void onClick(DialogInterface dialog, int which){
					       Toast.makeText(MainPad.this, "您已经选择了: "+ areas[which],Toast.LENGTH_LONG).show();
					       dialog.dismiss();
					       
					       Intent intent=new Intent(getApplicationContext(),StoreInformation.class);
					       intent.putExtra("ID", indexs[which].intValue());
					       startActivity(intent);
					       
					      }
					   }).show();
				}
				else{
					Toast.makeText(MainPad.this, "查找不到相关门店",Toast.LENGTH_LONG).show();
				}
				
			}
		});
        
        
        
		
    }
    
    
    
    
    
    
    
    
    
    /**
	 * 底板点击监听
	 */
	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}
		@Override
		public void onClick(View v) {
			mTabPager.setCurrentItem(index);
		}
	};
    
	
	
	
	
	
	
	
	
	
	public class MyOnPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				mTab1.setImageDrawable(getResources().getDrawable(R.drawable.foot1press));
				if (currIndex == 1) {
					animation = new TranslateAnimation(one, 0, 0, 0);
					mTab2.setImageDrawable(getResources().getDrawable(R.drawable.foot2));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, 0, 0, 0);
					mTab3.setImageDrawable(getResources().getDrawable(R.drawable.foot3));
				}
				else if (currIndex == 3) {
					animation = new TranslateAnimation(three, 0, 0, 0);
					mTab4.setImageDrawable(getResources().getDrawable(R.drawable.foot4));
				}
				else if (currIndex == 4) {
					animation = new TranslateAnimation(four, 0, 0, 0);
					mTab5.setImageDrawable(getResources().getDrawable(R.drawable.foot5));
				}
				break;
			case 1:
				mTab2.setImageDrawable(getResources().getDrawable(R.drawable.foot2press));
				if (currIndex == 0) {
					animation = new TranslateAnimation(zero, one, 0, 0);
					mTab1.setImageDrawable(getResources().getDrawable(R.drawable.foot1));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, one, 0, 0);
					mTab3.setImageDrawable(getResources().getDrawable(R.drawable.foot3));
				}
				else if (currIndex == 3) {
					animation = new TranslateAnimation(three, one, 0, 0);
					mTab4.setImageDrawable(getResources().getDrawable(R.drawable.foot4));
				}
				else if (currIndex == 4) {
					animation = new TranslateAnimation(four, one, 0, 0);
					mTab5.setImageDrawable(getResources().getDrawable(R.drawable.foot5));
				}
				break;
			case 2:
				mTab3.setImageDrawable(getResources().getDrawable(R.drawable.foot3press));
				if (currIndex == 0) {
					animation = new TranslateAnimation(zero, two, 0, 0);
					mTab1.setImageDrawable(getResources().getDrawable(R.drawable.foot1));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(one, two, 0, 0);
					mTab2.setImageDrawable(getResources().getDrawable(R.drawable.foot2));
				}
				else if (currIndex == 3) {
					animation = new TranslateAnimation(three, two, 0, 0);
					mTab4.setImageDrawable(getResources().getDrawable(R.drawable.foot4));
				}
				else if (currIndex == 4) {
					animation = new TranslateAnimation(four, two, 0, 0);
					mTab5.setImageDrawable(getResources().getDrawable(R.drawable.foot5));
				}
				break;
			case 3:
				mTab4.setImageDrawable(getResources().getDrawable(R.drawable.foot4press));
				if (currIndex == 0) {
					animation = new TranslateAnimation(zero, three, 0, 0);
					mTab1.setImageDrawable(getResources().getDrawable(R.drawable.foot1));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(one, three, 0, 0);
					mTab2.setImageDrawable(getResources().getDrawable(R.drawable.foot2));
				}
				else if (currIndex == 2) {
					animation = new TranslateAnimation(two, three, 0, 0);
					mTab3.setImageDrawable(getResources().getDrawable(R.drawable.foot3));
				}
				else if (currIndex == 4) {
					animation = new TranslateAnimation(four, three, 0, 0);
					mTab5.setImageDrawable(getResources().getDrawable(R.drawable.foot5));
				}
				break;
			case 4:
				mTab5.setImageDrawable(getResources().getDrawable(R.drawable.foot5press));
				if (currIndex == 0) {
					animation = new TranslateAnimation(zero, four, 0, 0);
					mTab1.setImageDrawable(getResources().getDrawable(R.drawable.foot1));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(one, four, 0, 0);
					mTab2.setImageDrawable(getResources().getDrawable(R.drawable.foot2));
				}
				else if (currIndex == 2) {
					animation = new TranslateAnimation(two, four, 0, 0);
					mTab3.setImageDrawable(getResources().getDrawable(R.drawable.foot3));
				}
				else if (currIndex == 3) {
					animation = new TranslateAnimation(three, four, 0, 0);
					mTab4.setImageDrawable(getResources().getDrawable(R.drawable.foot4));
				}
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(150);
			mTabImg.startAnimation(animation);
		}
		
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}
	
	
	

//设置listitem按钮的作用
	public void AListActivity(View v) {
		Intent intent = new Intent (MainPad.this,AListActivity.class);			
		startActivity(intent);
      }
	
	public void CListActivity(View v) {
		Intent intent = new Intent (MainPad.this,CListActivity.class);			
		startActivity(intent);
      }
	
	
	public void test(View v){
		Toast.makeText(MainPad.this, DataApplication.currentUser.getIMEI(),Toast.LENGTH_LONG).show();
	}
	
    
}
